﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;
using BL;
using BL.Model.Request;
using Common;
using DAL.Model;

namespace UI.MainFunctions
{
    public partial class frmReceiptManager : Base.DialogViewBase
    {
        private readonly MasterSession _session;
        private readonly ReceiptController _controller;
        private readonly CustomerController _customerController;

        #region Constructor

        public frmReceiptManager()
        {
            InitializeComponent();
            _session = new MasterSession();
            _controller = new ReceiptController(_session);
            _customerController = new CustomerController(_session);
            InitForm();
        }

        #endregion

        #region Overide methods

        protected override void FormatPrecision()
        {
            base.FormatPrecision();

            PrecisionHelper.FormatCurencyPrecision(gridColumn3,
                                                   gridColumn6,
                                                   gridColumn7);
        }

        #endregion

        #region Events

        protected override void InitializeBO()
        {
            base.InitializeBO();

            PrecisionHelper.FormatCurencyPrecision(gridColumn3,
                                                   gridColumn6,
                                                   gridColumn7);
        }

        protected override void PopulateStaticData()
        {
            base.PopulateStaticData();
            LoadCustomer();
        }

        protected override void PopulateData()
        {
            base.PopulateData();
            LoadGrid();
        }

        private void btnFind_Click(object sender, EventArgs e)
        {
            PopulateData();
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            var receipt = new frmReceipt();

            var result = receipt.ShowDialog();
            if (result == DialogResult.Yes)
                LoadGrid();
            else
                receipt.Dispose();
        }

        private void btnView_Click(object sender, EventArgs e)
        {
            var icCell = gridView2.GetFocusedRowCellValue("Id");
            if (icCell != null)
            {
                var receipt = new frmReceipt { ReceiptId = int.Parse(icCell.ToString()) };
                var result = receipt.ShowDialog();
                if (result == DialogResult.No) receipt.Dispose();
            }
        }

        protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
        {
            switch (keyData)
            {
                case Keys.F3:
                    btnSearch.PerformClick();
                    return true;

                case Keys.F5:
                    btnAdd.PerformClick();
                    return true;

            }

            return base.ProcessCmdKey(ref msg, keyData);
        }

        #endregion

        #region Function

        void InitForm()
        {
            cboCustomers.Text = "";
            dpFromDate.DateTime = DateTime.Now.AddDays(-7);
            dpToDate.DateTime = DateTime.Now;
        }

        void LoadCustomer()
        {
            var request = new SearchingRequest
            {
                Keyword = string.Empty,
                IsPaging = false
            };

            var customers = _customerController.Search(request);
            customers.Insert(0, new Customer { Id = -1, Name = string.Empty });

            cboCustomers.Properties.DataSource = customers;
            cboCustomers.Properties.DisplayMember = "Name";
            cboCustomers.Properties.ValueMember = "Id";
        }

        public void LoadGrid()
        {
            try
            {
                var customerIds = new List<int>();
                var ids = new List<int>();
                var item = cboCustomers.EditValue;
                if (item != null && !string.IsNullOrEmpty(item.ToString()))
                {
                    customerIds.Add(int.Parse(item.ToString()));
                }
                else
                {
                    customerIds.Add(Constant.AllValue);
                }
                ids.Add(!string.IsNullOrEmpty(txtReceiptNo.Text) ? int.Parse(txtReceiptNo.Text) : Constant.AllValue);

                if (!string.IsNullOrEmpty(dpFromDate.Text) && !string.IsNullOrEmpty(dpToDate.Text) && dpFromDate.DateTime > dpToDate.DateTime)
                {
                    MessageBox.Show("Nhập lại ngày cần tìm", "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }

                var receiptAmount = Convert.ToDecimal(txtAmount.Text);

                var request = new SearchingReceiptRequest
                {
                    DocumentNo =  txtReceiptNo.Text,
                    FromTime = dpFromDate.DateTime,
                    ToTime = dpToDate.DateTime.AddDays(1).AddSeconds(-1),
                    CustomerIds = customerIds,
                    IsPaging = false,
                    Ids = ids,
                    FromAmount = receiptAmount
                };

                var lst = _controller.Search(request);
                gridReceipt.DataSource = lst;
                gridView2.BestFitColumns();
            }
            catch (Exception ex) { }
        }

        #endregion
    }
}
